//
//  BusListViewController.m
//  安好
//
//  Created by lianchuang on 14-8-14.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "BusListViewController.h"
#import "BusListCell.h"
@interface BusListViewController ()

@end

@implementation BusListViewController
{
    UILabel *taxiCostLabel;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title=@"公交路线";
    self.view.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    //tableview的创建，以及布局
    self.tableview=[[UITableView alloc]init];
    CGFloat height;
    if (kScreenWidth==414) {
        height=12;
    }else{
        height=0;
    }
    self.tableview.frame=CGRectMake(0, 0, kScreenWidth, kScreenHeight-64-59-height);
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    
    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, kScreenHeight-64-58.5-height, kScreenWidth, 58.5+height)];
    imageview.backgroundColor=[ClassMethodsViewController colorWithHexString:NACIGATIONBAR_BACKGROUNDCOLOR_16];
    [self.view addSubview:imageview];
    taxiCostLabel=[[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth==414?20: 15, kScreenHeight-64-height-58+(height+58.5-20)/2, kScreenWidth, 20)];
    
    taxiCostLabel.font=[UIFont systemFontOfSize:LABEL_FONT_SIZE_TITLE];
    taxiCostLabel.text=[NSString stringWithFormat:@"打车约%@元",self.taxiCost];
    taxiCostLabel.textColor=[UIColor whiteColor];
    [self.view addSubview:taxiCostLabel];
    if ( self.allBusLists.count<=0) {
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,(kScreenHeight-64-20-58-height)/2, kScreenWidth,20)];
        label.text=@"暂无公交线路";
//        label.center=self.view.center;
        label.textColor=[UIColor darkGrayColor];
        label.textAlignment=NSTextAlignmentCenter;
        label.backgroundColor=[UIColor clearColor];
        [self.view addSubview:label];
    }
    [self setnarBar];
}
- (void)setnarBar
{
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
}
-(void)backClicked
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma  mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allBusLists.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier=@"cell";
    BusListCell *buscell=[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (buscell==nil) {
        
        if (kScreenWidth==414) {
           buscell=[[[NSBundle mainBundle] loadNibNamed:@"BusListCell" owner:self options:nil] lastObject];
        }else{
            buscell=[[[NSBundle mainBundle] loadNibNamed:@"BusListCell" owner:self options:nil] objectAtIndex:0];
        }
    }
    //公交车
    buscell.busName.text=[self appendBusStops:[self.allBusLists objectAtIndex:indexPath.row]];
    //详细信息
    NSMutableString *string=[NSMutableString stringWithString:[self changeTimeType:[[[self.allBusLists objectAtIndex:indexPath.row] valueForKey:@"duration"] integerValue]]];
    [string appendString:[NSString stringWithFormat:@"|步行%@米",[[self.allBusLists objectAtIndex:indexPath.row] valueForKey:@"walkingDistance"]]];
    buscell.detailLabel.text=string;
    buscell.type.layer.cornerRadius=2;
    buscell.selectionStyle=UITableViewCellSelectionStyleNone;
    return buscell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth==414) {
        return 72;
    }
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.delegate&&[self.delegate respondsToSelector:@selector(reloadBusLine:)]) {
            [self.delegate reloadBusLine:[self.allBusLists objectAtIndex:indexPath.row]];
        }
    }];
}
//写了一个方法  功能是：将传入的公交stop进行截取 再将所有stop拼接  展示
- (NSString *)appendBusStops:(NSDictionary *)dict
{
    NSString *stops;
    
    NSArray *array=[dict valueForKey:@"segments"];
    
    //数组第0个
    NSString *name=[[[array objectAtIndex:0] valueForKey:@"busline"] valueForKey:@"name"];
    NSRange seachRange=[name rangeOfString:@"("];
    stops=[name substringToIndex:seachRange.location];
    for (int i=1; i<array.count; i++) {
        NSString *name=[[[array objectAtIndex:i] valueForKey:@"busline"] valueForKey:@"name"];
        NSRange seachRange=[name rangeOfString:@"("];
        if (seachRange.location>0) {
            stops=[stops stringByAppendingString:[NSString stringWithFormat:@"—>%@",[name substringToIndex:seachRange.location]]];
        }
    }
    return stops;
}
//写了一个方法  功能是：传入一个时间（秒）转换成  几天几小时几分钟的形式
- (NSString *)changeTimeType:(NSInteger)time
{
    NSString *changeTime;
    NSInteger day;
    NSInteger hours;
    NSInteger minutes;
    
    day=time/(3600*24);
    hours=time%(3600*24)/3600;
    minutes=time%(3600*24)%3600/60;
    if (day<1) {
        if (hours<1) {
            changeTime=[NSString stringWithFormat:@"约%ld分",minutes];
        }else
        {
            changeTime=[NSString stringWithFormat:@"约%ld小时%ld分",hours,minutes];
        }
    }else
    {
        if (hours<1) {
            changeTime=[NSString stringWithFormat:@"约%ld天%ld小时%ld分",hours,hours,minutes];
        }else
        {
            changeTime=[NSString stringWithFormat:@"约%ld天%ld小时%ld分",day,hours,minutes];
        }
    }
    return changeTime;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
